package com.example.shengsheng;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.shengsheng.activity.RecyclerView1Activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    TextView textView1 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }


        textView1 = findViewById(R.id.normaltext1);
        Log.d("onCreate", "1");

        String content = textView1.getText().toString();
        Log.d("onCreate", content);
    }

    public void onClick1(View view) {
        Log.d("onClick1", "2");
//        textView.setText("shibai");

        System.setProperty("http.proxyHost", "192.168.2.102");
        System.setProperty("http.proxyPort", "18888");

        // ==============================================================
        // 主线程中的同步请求

        //1.创建OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //2.创建Request对象，设置一个url地址（百度地址）,设置请求方式。
        Request request = new Request.Builder().url("http://sd.jumpcity.cm/api/v1.test/aa").method("GET",null).build();
        //3.创建一个call对象,参数就是Request请求对象
        Call call = okHttpClient.newCall(request);
        try {
            Log.d("box", "onResponse 11");
            //同步调用,返回Response,会抛出IO异常
            Response response = call.execute();
//            Log.d("box", response.body().string());
            Log.d("box", "onResponse 12");

            String responseData = response.body().string();
            Log.d("box", responseData);

            Log.d("box", "onResponse 13");
            textView1.setText("chenggong");

            try{
                JSONObject jsonObject = new JSONObject(responseData);
                int errcode = jsonObject.getInt("errcode");
                String message = jsonObject.getString("message");
                Log.d("errcode", "" + errcode);
                Log.d("message", message);
                JSONObject jsonObject2 = jsonObject.getJSONObject("data");
                JSONArray jsonArray = jsonObject2.getJSONArray("list");

//                JSONArray jsonArray = new JSONArray(responseData);
                for(int i = 0;i < jsonArray.length(); i++)
                {
                    JSONObject jsonObjectX = jsonArray.getJSONObject(i);
                    int id = jsonObjectX.getInt("id");
                    String name = jsonObjectX.getString("name");

                    Log.d("item", "" + id + "-" + name);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
            textView1.setText("exception");
            Log.d("box", "onResponse 2");
        }

        // ==============================================================

        /*
        OkHttpClient okHttpClient = new OkHttpClient();//把客户端new出来
        Request.Builder builder = new Request.Builder();//请求建立对象
        try {
            Request request = builder
                    .get()//"GET"方式请求网络
                    .url("http://sd.jumpcity.cm/api/v1.test/aa")
                    .build();//请求对象建立请求
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {//入队
                @Override
                public void onFailure(Call call, IOException e) {
                    //当失败时
                    Log.d("box", e.getMessage());
                    Log.d("box", "onFailure");
                    textView1.setText("shibai");
                }

                @Override
                public void onResponse(Call call, final Response response) throws IOException {
//                    当响应时
                    runOnUiThread(new Thread(){//在主线程更新UI
                        @Override
                        public void run() {
                            try {
//                                mTextView.setText(response.body().string());//从网络上获取的就是JSON
                                Log.d("box", response.body().string());
                                Log.d("box", "onResponse 1");
                                textView1.setText("chenggong");
                            } catch (IOException e) {
                                e.printStackTrace();
                                textView1.setText("exception");
                                Log.d("box", "onResponse 2");
                            }
                        }
                    });

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        */
    }

    public void onClick2(View view) {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(intent);
    }

    public void onClick3(View view) {
        Intent intent = new Intent(MainActivity.this, RecyclerView1Activity.class);
        startActivity(intent);
    }
}
